﻿
//	------------------------------------------------------------------------------
//                                       _                          
//              _ __ ___   ___ _ __ ___ | |__  _ __ __ _ _ __   ___ 
//             | '_ ` _ \ / _ \ '_ ` _ \| '_ \| '__/ _` | '_ \ / _ \
//             | | | | | |  __/ | | | | | |_) | | | (_| | | | |  __/
//             |_| |_| |_|\___|_| |_| |_|_.__/|_|  \__,_|_| |_|\___|
//                                                     
//	------------------------------------------------------------------------------

#region - License -
//
//	------------------------------------------------------------------------------                                                   
//	         Copyright (c) 2010 Peter Vyvey (peter.vyvey@chiliware.be)
//	------------------------------------------------------------------------------
//
//
//	This file is part of CHILIWARE MEMBRANE.
//
//	CHILIWARE MEMBRANE is free software: you can redistribute it and/or modify
//	it under the terms of the GNU Lesser General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	CHILIWARE MEMBRANE is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU Lesser General Public License for more details.
//
//	You should have received a copy of the GNU Lesser General Public License
//	along with CHILIWARE MEMBRANE.  If not, see <http://www.gnu.org/licenses/>.
//
//
#endregion

#region - Changes -
//
//	peter.vyvey@chiliware.be		2010-08-23		Created
//
#endregion

#region - Using -

#endregion

namespace Chiliware.Membrane.Pattern.Creational
{
	#region - Singleton -

	/// <summary>
	/// Generic static class to host any object with a parameterless constructor as a singleton.
	/// </summary>
	/// <typeparam name="T">Type to be hosted as a singleton.</typeparam>
	public static class Singleton<T> 
		where T : class, new()
	{
		#region - Nested classes: Singleton.SingletonCreator -

		/// <summary>
        /// Internal class containing the actual singleton instance.
        /// </summary>
        private static class SingletonCreator
        {
            #region - Static constructors -

            /// <summary>
            /// Static constructor.
            /// </summary>
            static SingletonCreator() { }

            #endregion

            #region - Internal static fields -

            /// <summary>
            /// Field to hold the actual instance of the singleton.
            /// </summary>
            internal static readonly T Instance = new T();

            #endregion
        }

        #endregion

        #region - Static constructors -

        /// <summary>
        /// Static constructor.
        /// </summary>
        static Singleton() { }

        #endregion

		#region - Static properties -

		/// <summary>
		/// Publicly available property holding the singleton instance.
		/// </summary>
		public static T Instance
		{
            get { return SingletonCreator.Instance; }
		}

		#endregion
	}

	#endregion
}
